package constant.milk.toilet.tip;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import constant.milk.toilet.R;
import constant.milk.toilet.SuperActivity;
import constant.milk.toilet.util.MilkUtil;

public class TipActivity extends SuperActivity
{
	private TextView preView;
	private TextView titleTextView;
	
	private ViewPager pagerView;
	
	private TextView oneTextView;
	private TextView twoTextView;
	private TextView threeTextView;
	private TextView fourTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tip_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}
		
	@Override
	protected void setFindViewId()
	{
		preView = (TextView) findViewById(R.id.tipPreTextView);
		titleTextView = (TextView) findViewById(R.id.tipTitleTextView);
		
		pagerView = (ViewPager) findViewById(R.id.tipPagerView);
		
		oneTextView = (TextView) findViewById(R.id.tipOneTextView);
		twoTextView = (TextView) findViewById(R.id.tipTwoTextView);
		threeTextView = (TextView) findViewById(R.id.tipThreeTextView);
		fourTextView = (TextView) findViewById(R.id.tipFourTextView);
	}

	@Override
	protected void setOnClick()
	{
		preView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		
		oneTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pagerView.setCurrentItem(0);
			}
		});
		
		twoTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pagerView.setCurrentItem(1);
			}
		});
		
		threeTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pagerView.setCurrentItem(2);
			}
		});
		
		fourTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				pagerView.setCurrentItem(3);
			}
		});
		
		pagerView.setOnPageChangeListener(new OnPageChangeListener()
		{
			@Override
			public void onPageSelected(int arg0)
			{
				if (arg0 == 0)
				{
					oneTextView.setBackgroundResource(R.drawable.button_tip_push);
					twoTextView.setBackgroundResource(R.drawable.button_tip_def);
					threeTextView.setBackgroundResource(R.drawable.button_tip_def);
					fourTextView.setBackgroundResource(R.drawable.button_tip_def);
				}
				else if (arg0 == 1)
				{
					oneTextView.setBackgroundResource(R.drawable.button_tip_def);
					twoTextView.setBackgroundResource(R.drawable.button_tip_push);
					threeTextView.setBackgroundResource(R.drawable.button_tip_def);
					fourTextView.setBackgroundResource(R.drawable.button_tip_def);
				}
				else if (arg0 == 2)
				{
					oneTextView.setBackgroundResource(R.drawable.button_tip_def);
					twoTextView.setBackgroundResource(R.drawable.button_tip_def);
					threeTextView.setBackgroundResource(R.drawable.button_tip_push);
					fourTextView.setBackgroundResource(R.drawable.button_tip_def);
				}
				else if (arg0 == 3)
				{
					oneTextView.setBackgroundResource(R.drawable.button_tip_def);
					twoTextView.setBackgroundResource(R.drawable.button_tip_def);
					threeTextView.setBackgroundResource(R.drawable.button_tip_def);
					fourTextView.setBackgroundResource(R.drawable.button_tip_push);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2)
			{
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0)
			{
			}
		});
	}

	@Override
	protected void setData()
	{
		titleTextView.setTextColor(Color.WHITE);
		oneTextView.setTextColor(Color.WHITE);
		twoTextView.setTextColor(Color.WHITE);
		threeTextView.setTextColor(Color.WHITE);
		
		titleTextView.setTextSize(TEXT_SIZE + 2);
		
		TipPagerAdapter adapter = new TipPagerAdapter();
		pagerView.setAdapter(adapter);
//		pagerView.setCurrentItem(0);
	}
	
	private class TipPagerAdapter extends PagerAdapter
	{
		public TipPagerAdapter(){};
		
		@Override
		public int getCount()
		{
			return 4;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position)
		{
			ViewGroup v = null;
			
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			if (position == 0)
			{
				v = (ViewGroup) inflater.inflate(R.layout.tip_sticker_activty, null);
			}
			else if (position == 1)
			{
				v = (ViewGroup) inflater.inflate(R.layout.tip_one_activty, null);
			}
			else if (position == 2)
			{
				v = (ViewGroup) inflater.inflate(R.layout.tip_two_activty, null);
				
				TextView tv1 = (TextView) v.findViewById(R.id.tipTwo1TextView);
				final View v1 = v.findViewById(R.id.tipTwo1View);
				
				TextView tv2 = (TextView) v.findViewById(R.id.tipTwo2TextView);
				final View v2 = v.findViewById(R.id.tipTwo2View);
				
				TextView tv3 = (TextView) v.findViewById(R.id.tipTwo3TextView);
				final View v3 = v.findViewById(R.id.tipTwo3View);
				
				tv1.setVisibility(View.VISIBLE);
				tv2.setVisibility(View.VISIBLE);
				tv3.setVisibility(View.VISIBLE);
				
				v1.setVisibility(View.GONE);
				v2.setVisibility(View.GONE);
				v3.setVisibility(View.GONE);
				
				tv1.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Animation ani = AnimationUtils.loadAnimation(superContext, R.anim.up_down);
						v1.startAnimation(ani);
						
						v.setVisibility(View.GONE);
						v1.setVisibility(View.VISIBLE);
					}
				});
				
				tv2.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Animation ani = AnimationUtils.loadAnimation(superContext, R.anim.up_down);
						v2.startAnimation(ani);
						
						v.setVisibility(View.GONE);
						v2.setVisibility(View.VISIBLE);
					}
				});
				
				tv3.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Animation ani = AnimationUtils.loadAnimation(superContext, R.anim.up_down);
						v3.startAnimation(ani);
						
						v.setVisibility(View.GONE);
						v3.setVisibility(View.VISIBLE);
					}
				});
			}
			else if (position == 3)
			{
				v = (ViewGroup) inflater.inflate(R.layout.tip_three_activty, null);
			}
			
			pagerView.addView(v, 0);
			
			return v;
		}
		
		@Override        
		public void destroyItem(View pager, int position, Object view) 
		{                
			((ViewPager)pager).removeView((View)view);        
		}                 
		
		@Override        
		public boolean isViewFromObject(View pager, Object obj) 
		{            
			return pager == obj;         
		}
	}
	
}
